//
//  MoreTableVC.m
//  DaoZiJinFu
//
//  Created by woker on 16/6/28.
//  Copyright © 2016年 iwoker. All rights reserved.
//

#import "MoreTableVC.h"
#import "AssetCell.h"
#import "SafetyViewController.h"
#import "RecentAnnounce.h"
#import "FAQTableVC.h"
#import "AboutUs.h"
#import "UserInfoTableVC.h"
#import "MoreUserInfoCell.h"
#import "MoreFooterView.h"
#import "HotPhoneView.h"
#import "LZSecutyViewController.h"
#import "DZCustomerView.h"
@interface MoreTableVC () {
    NSArray *titleArray;
    HotPhoneView *hotPhone;
}

@end

@implementation MoreTableVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTitleView:@"更多"];
    self.tabBarItem.title = @"更多";
    self.view.backgroundColor = BACKGROUND_COLOR;
    self.tableView.backgroundView = nil;
    self.tableView.backgroundColor = BACKGROUND_COLOR;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    titleArray = @[@"安全保障",@"最新公告",@"常见问题",@"关于我们"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"AssetCell" bundle:nil] forCellReuseIdentifier:ASSET_CELL];
    [self.tableView registerNib:[UINib nibWithNibName:@"MoreUserInfoCell" bundle:nil] forCellReuseIdentifier:MOREUSER_CELL];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showHotPhone) name:@"hotPhoneNotification" object:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)loadForUserInfo {
    DZRequestUtil *request = [[DZRequestUtil alloc] init];
    request.httpMethod = @"GET";
    request.requestUrl = @"User/getUserInfo";
    [DZNetAPI request:request success:^(id response) {
        [self.tableView.mj_header endRefreshing];
        DZResponse *model = [DZResponse mj_objectWithKeyValues:response];
        if (model.rt == 1) {
            [CommTools setUserInfo:response];
            [self.tableView reloadData];
        }else if (model.rt == 11){
        }else if (model.rt == 0) {
            [self getLoginAction];
        }else{
            JSSHOW(model.msg);
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)getLoginAction {
    if (DZUserInfo.userInfo.mobile) {
        DZRequestUtil *request = [[DZRequestUtil alloc] init];
        request.requestUrl = @"Index/login";
        request.params = @{@"mobile":DZUserInfo.userInfo.mobile};
        [DZNetAPI request:request success:^(id response) {
            NSLog(@"%@",response);
            DZResponse *info = [DZResponse mj_objectWithKeyValues:response];
            if (info.rt == 1) {
                [theUser setObject:info.data[LOGIN_TOKEN] forKey:LOGIN_TOKEN];
                [theUser setObject:[NSNumber numberWithBool:YES] forKey:ISLOGIN];
                [CommTools setUserInfo:response];
            }else {
                JSSHOW(info.msg);
                 [theUser setObject:[NSNumber numberWithBool:NO] forKey:ISLOGIN];
                [CommTools removeAllSet];
               
            }
            
            [self.tableView reloadData];
        } failure:^(NSError *error) {
        }];
    }
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (!section) {
        return 1;
    }
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (!indexPath.section) {
        return 84;
    }
    return 54;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 8;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 1) {
        return 135;
    }
    return 1;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 1) {
        DZCustomerView *view = [DZCustomerView loadView];
        view.phoneNumber.text = @"400 - 666 - 1280";
        view.times.text = @"每天9:00 - 21:00";
        return view;
    }
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    if (!indexPath.section) {
        MoreUserInfoCell *userCell = [MoreUserInfoCell cellWithTableView:tableView];
        userCell.userTel.text = @"";
        userCell.userName.text = @"";
        if (DZUserInfo.userInfo.mobile) {
            NSMutableString *telStr = [NSMutableString stringWithString:DZUserInfo.userInfo.mobile];
            [telStr replaceCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
            userCell.userTel.text = telStr;
        }
        if (DZUserInfo.userInfo.nick) {
            userCell.userName.text = DZUserInfo.userInfo.nick;
        }
        [userCell.userIcon sd_setImageWithURL:[NSURL URLWithString:DZUserInfo.userInfo.head_img_name] placeholderImage:[UIImage imageNamed:@"默认头像"]];
        cell = userCell;
    } else {
        AssetCell *assetCell = [AssetCell cellWithTableView:tableView];
        assetCell.cellIcon.image = [UIImage imageNamed:titleArray[indexPath.row]];
        assetCell.cellTitle.text = titleArray[indexPath.row];
        assetCell.cellSubTitle.hidden = YES;
        assetCell.lines.hidden = NO;
        if (indexPath.row == 1 && [DZUserInfo.userInfo.unread_bulletin_number integerValue] != 0) {
            assetCell.point.hidden = NO;
        } else {
            assetCell.point.hidden = YES;
        }
        cell = assetCell;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIViewController *viewController;
    viewController.hidesBottomBarWhenPushed = YES;
    if (!indexPath.section) {
        if (userValue(ISLOGIN) && [userValue(ISLOGIN) boolValue] == YES) {
            UserInfoTableVC *userInfo = [[UserInfoTableVC alloc] init];
            viewController = userInfo;
        } else {
            if (userValue(HOSTTEL)) {
                LoginViewController *login = [[LoginViewController alloc] init];
                login.telNum = userValue(HOSTTEL);
                viewController = login;
            } else {
                RegisterViewController *join = [[RegisterViewController alloc] init];
                viewController = join;
            }
        }
    } else {
        if (indexPath.row == 0) {
            LZSecutyViewController *safety = [[LZSecutyViewController alloc] init];
            safety.flag = 1;
            viewController = safety;
        } else if (indexPath.row == 1) {
            if ([userValue(ISLOGIN) boolValue]) {
                RecentAnnounce *announce = [[RecentAnnounce alloc] init];
                viewController = announce;
            } else {
                if (userValue(HOSTTEL)) {
                    LoginViewController *login = [[LoginViewController alloc] init];
                    login.telNum = userValue(HOSTTEL);
                    viewController = login;
                } else {
                    RegisterViewController *join = [[RegisterViewController alloc] init];
                    viewController = join;
                }
            }
        } else if (indexPath.row == 2) {
            FAQTableVC *FAQ = [[FAQTableVC alloc] init];
            viewController = FAQ;
        } else if (indexPath.row == 3) {
            LZSecutyViewController *safety = [[LZSecutyViewController alloc] init];
            safety.flag = 2;
            viewController = safety;
        }
    }
    if (viewController) {
        viewController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:viewController animated:YES];
    }
}

- (void)showHotPhone {
    NSString *allString = [NSString stringWithFormat:HOTTel];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:allString]];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if ([userValue(ISLOGIN) boolValue] == YES) {
        self.tableView.mj_header = [MJDIYHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadForUserInfo)];
        [self loadForUserInfo];
    } else {
        self.tableView.mj_header = nil;
    }
    [self.tableView reloadData];
}

@end
